package com.zrkj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrkj.entity.TSysResource;
import com.zrkj.entity.vo.Result;
import com.zrkj.mapper.TSysResourceMapper;
import com.zrkj.service.ITSysResourceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author : 国良
 * @className : ITSysResourceServiceImpl
 * @package: com.ha.service.impl
 * @description : 资源表 服务接口实现
 * @date :  2021-05-14
 */
@Service("iTSysResourceService")
public class ITSysResourceServiceImpl extends ServiceImpl<TSysResourceMapper, TSysResource> implements ITSysResourceService {
    /**
     * 添加
     *
     * @param tSysResource
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<String> add(TSysResource tSysResource) {
        return Result.success(this.save(tSysResource));
    }

    /**
     * 更新
     *
     * @param tSysResource
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Boolean> update(TSysResource tSysResource) {
        return Result.success(this.updateById(tSysResource));
    }

    /**
     * 删除
     *
     * @param idList
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Boolean> delete(List<Long> idList) {
        return Result.success(this.removeByIds(idList));
    }

    /**
     * 查询详情
     *
     * @param id
     * @return
     */
    @Override
    public Result<TSysResource> getById(Long id) {
        return Result.success(baseMapper.selectById(id));
    }

    /**
     * 查询列表
     *
     * @param tSysResource
     * @return
     */
    @Override
    public Result<List<TSysResource>> queryList(TSysResource tSysResource) {
        QueryWrapper<TSysResource> queryWrapper = buildQuery(tSysResource);
        return Result.success(this.list(queryWrapper));
    }


    public Result<IPage<TSysResource>> queryPageList(TSysResource data) {
        QueryWrapper<TSysResource> queryWrapper = buildQuery(data);
        IPage<TSysResource> pageQuery = this.page(data.getPage(), queryWrapper);
        return Result.success(pageQuery);
    }

    private QueryWrapper<TSysResource> buildQuery(TSysResource data){
        QueryWrapper<TSysResource> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(data.getId()),"`id`",data.getId());
        queryWrapper.in(!CollectionUtils.isEmpty(data.getIds()),"`id`",data.getIds());
        queryWrapper.eq(StringUtils.isNotBlank(data.getName()),"`name`",data.getName());
        queryWrapper.eq(StringUtils.isNotBlank(data.getType()),"`type`",data.getType());
        queryWrapper.eq(StringUtils.isNotBlank(data.getParentId()),"parent_id",data.getParentId());
        queryWrapper.orderByAsc("`order`");
        return queryWrapper;
    }

    public Result getAllReasource(){
        TSysResource resource = new TSysResource();
        resource.setParentId("0");
        List<TSysResource> list = getAll(resource);
        if(CollectionUtils.isEmpty(list)){
            return Result.success();
        }
        digui(list);
        return Result.success(list);
    }

    private void digui(List<TSysResource> list) {
        for (TSysResource tSysResource : list) {
            TSysResource con = new TSysResource();
            con.setParentId(tSysResource.getId());
            List<TSysResource> all = getAll(con);
            if(!CollectionUtils.isEmpty(all)){
                tSysResource.getChildren().addAll(all);
                digui(all);
            }
        }
    }

    private List<TSysResource> getAll(TSysResource tSysResource) {
        QueryWrapper<TSysResource> queryWrapper = buildQuery(tSysResource);
        return this.list(queryWrapper);
    }


}
